package inet.app.controller.parser;


import inet.app.controller.connect.JsonParser;
import inet.app.model.object.Tk_Item;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

public class Parser_Tk {


	private ArrayList<Tk_Item> list = new ArrayList<Tk_Item>();
	private Tk_Item item;
	private String ITEMS = "items";
	private String ITEM="item";
	private String CODE ="code";
	private String NAME = "name";
	private String NUMBERDATE = "numberdate";
	private String NUMBERCOUPLE = "numbercouple";
	private String TKCS = "tkcs";
	private String MOST = "most";
	private String LEAST = "least";
	//Add Tai Loc Phat
	private String SELECT="select";
	private String NUMBER = "number";
	private String DATE = "date";
	private String CHUARA = "chuara";
	private String CUCDAI = "cucdai";
	
	public Parser_Tk () {
		
	}
	public ArrayList<Tk_Item> getList(String url) {
		list = new ArrayList<Tk_Item>();
		JSONObject jobject,object,job_items;
		JsonParser jParser = new JsonParser();
		jobject = jParser.getJSONFromUrl(url);
		try {
				String items = jobject.getString(ITEMS);
				job_items = new JSONObject(items);
				String code= job_items.getString(CODE);
				String name= job_items.getString(NAME);
				String numberdate= job_items.getString(NUMBERDATE);
				String numbercouple= job_items.getString(NUMBERCOUPLE);
				String tkcs= "";
				System.out.println("=====>> TKCS : " + items);
				String most= job_items.getString(MOST);
				String least= job_items.getString(LEAST);
				
				item = new Tk_Item(code, name, numberdate, numbercouple, tkcs, most, least);
				list.add(item);
			return list;
        } catch (Exception e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
            return null;
        }
		
	}
	public ArrayList<Tk_Item> getListTK(String url) {
		list = new ArrayList<Tk_Item>();
		JSONObject jobject,job_items;
		
		try {
			jobject = new JSONObject(url);
				String items = jobject.getString(ITEMS);
				job_items = new JSONObject(items);
				String code= job_items.getString(CODE);
				String name= job_items.getString(NAME);
				String numberdate= job_items.getString(NUMBERDATE);
				String numbercouple= job_items.getString(NUMBERCOUPLE);
				String tkcs= "";
				System.out.println("=====>> TKCS : " + items);
				String most= job_items.getString(MOST);
				String least= job_items.getString(LEAST);
				item = new Tk_Item(code, name, numberdate, numbercouple, tkcs, most, least);
				list.add(item);
			return list;
        } catch (Exception e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
            return null;
        }
		
	}

	public ArrayList<Tk_Item> getList_Cs(String url) {
		list = new ArrayList<Tk_Item>();
		JSONObject jobject,object,job_items;
		JsonParser jParser = new JsonParser();
		jobject = jParser.getJSONFromUrl(url);
		try {
				String items = jobject.getString(ITEMS);
				job_items = new JSONObject(items);
				String code= job_items.getString(CODE);
				String name= job_items.getString(NAME);
				String numberdate= job_items.getString(NUMBERDATE);
				String numbercouple= job_items.getString(NUMBERCOUPLE);
				String tkcs= job_items.getString(TKCS);
				System.out.println("=====>> TKCS : " + items);
				String most= job_items.getString(MOST);
				String least= job_items.getString(LEAST);
				
				item = new Tk_Item(code, name, numberdate, numbercouple, tkcs, most, least);
				list.add(item);
			return list;
        } catch (Exception e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
            return null;
        }
		
	}
	public ArrayList<Tk_Item> getList_CsTK(String url) {
		list = new ArrayList<Tk_Item>();
		JSONObject jobject,object,job_items;
		
		try {
			jobject = new JSONObject(url);
				String items = jobject.getString(ITEMS);
				job_items = new JSONObject(items);
				String code= job_items.getString(CODE);
				String name= job_items.getString(NAME);
				String numberdate= job_items.getString(NUMBERDATE);
				String numbercouple= job_items.getString(NUMBERCOUPLE);
				String tkcs= job_items.getString(TKCS);
				System.out.println("=====>> TKCS : " + tkcs);
				String most= job_items.getString(MOST);
				String least= job_items.getString(LEAST);
				
				item = new Tk_Item(code, name, numberdate, numbercouple, tkcs, most, least);
				list.add(item);
				System.out.println("LÍT trả vè htoong kê :" +list.get(0).getCode());
			return list;
        } catch (Exception e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
            return null;
        }
		
	}
	public ArrayList<Tk_Item> getListTKDB(String url) {
		list = new ArrayList<Tk_Item>();
		JSONObject jobject,object,job_items;
		try {
			jobject = new JSONObject(url);
				JSONArray ja = jobject.getJSONArray(ITEMS);
				int i = 0;
				String number,date,chuara,cucdai;
				
				for (i = 0; i < ja.length(); i++) {
					object = ja.getJSONObject(i);
					number = object.getString(NUMBER);
					date = object.getString(DATE);
					chuara = object.getString(CHUARA);
					
					item = new Tk_Item(number, date, chuara);
					list.add(item);
				}
			return list;
        } catch (Exception e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
            return null;
        }
		
	}
}
